import cfg
import start_end
import obj
import pygame
import sys
import random
import time

'''
黑屏显示第几关，每个关卡开始前都要调用
'''
def black_screen(screen,star,string):
    font = pygame.font.Font(cfg.FONT_BUTTON_PATH,cfg.FONT_BUTTON_SIZE)
    text = font.render(string,True,cfg.FONT_BUTTON_COLOR)
    text_rect = text.get_rect()
    text_rect.centerx,text_rect.centery = cfg.SCREEN_SIZE[0]/2,cfg.SCREEN_SIZE[1]/2

    time_start = pygame.time.get_ticks()
    while True:
        screen.fill((0,0,0))
        time_now = pygame.time.get_ticks()
        if time_now - time_start >= 2000:
            break
        screen.blit(text,text_rect)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == pygame.MOUSEMOTION:
                star.set_position(pygame.mouse.get_pos())
        star.draw(screen)
        pygame.display.update()



'''
第一关
    展示画面：第一关
    加载地图
    生成Hero、EnemyA * cfg.ENEMYA_NUM_ONE
    音乐
    定时事件：生成武器
    检测触碰(可放到object里面的类里去实现)
    人物死亡：跳转到end
    所有怪物死亡：跳转到level2
    '''
def level1(screen,star):
    # 显示第一关
    black_screen(screen,star,"第一关")
    # bgm and bgm
    bg_img = pygame.image.load(cfg.LEVEL1_BG_IMG_PATH)
    bg_img = pygame.transform.scale(bg_img,cfg.LEVEL1_BG_IMG_SIZE)
    bg_img_rect = bg_img.get_rect()
    pygame.mixer.music.load(cfg.BATTLE_BGM)
    pygame.mixer.music.play(-1)
    # 生成玩家
    hero = obj.Hero()
    hero.set_position((500,200))
    # 生成针管
    zhen = obj.Zhen(hero)
    # 生成怪物群
    monsters_live = set()
    for pos in cfg.MONSTERA_COORDS:
        monsterA = obj.MonsterA()
        monsterA.set_position(pos)
        monsters_live.add(monsterA)
    # 时钟  
    clock = pygame.time.Clock()
    # 子弹集合
    weas_live = set()
    # 设置随机生成的强力回旋镖
    appear_event = pygame.USEREVENT
    pygame.time.set_timer(appear_event, cfg.WEAPEN_1_TIME)

    exist = False # 存在静态回旋镖
    WEA = 0 # 静态回旋镖
    hold = False # 人物获得了回旋镖

    exist_ = False # 存在动态回旋镖
    WEA_ = 0 # 动态回旋镖
    
    status = 0 # 通关（1）或者失败（2）的标志
    while True:
        weas_dead = set() # 销毁的子弹：杀人而销毁、越界而销毁
        monsters_dead = set() # 死去的怪物
        screen.blit(bg_img,bg_img_rect)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == pygame.MOUSEMOTION:
                star.set_position(pygame.mouse.get_pos())
            elif event.type == pygame.MOUSEBUTTONDOWN:
                if event.button == 1:
                    pygame.mixer.Sound(cfg.BIU).play()
                    if hold:
                        WEA_ = obj.Weapen1()
                        WEA_.set_direction(zhen)
                        exist_ = True
                        hold = False
                    else:
                        new_weapen = obj.Weapen0()
                        new_weapen.set_direction(zhen)
                        weas_live.add(new_weapen)
            elif event.type == pygame.KEYDOWN:
                hero.press(event.key)
            elif event.type == pygame.KEYUP:
                hero.release(event.key)
            elif event.type == appear_event:
                if exist : break
                x = random.randint(100,cfg.SCREEN_SIZE[0]-100)
                y = random.randint(100,cfg.SCREEN_SIZE[1]-100)
                WEA = obj.Weapen_1_static((x,y))
                exist = True
        # 人物与静态回旋镖的互动：吃
        if exist:
            WEA.draw(screen)
            if pygame.sprite.collide_rect(WEA,hero):
                hold = True
                exist = False
        # 动态回旋镖的处理
        if exist_:
            WEA_.update()
            WEA_.draw(screen)
            # 回旋镖越界处理
            if WEA_.rect.bottom < 0 or WEA_.rect.top > cfg.SCREEN_SIZE[1] or WEA_.rect.left > cfg.SCREEN_SIZE[0] or WEA_.rect.right < 0:
                exist_ = False
            for mons in monsters_live:
                if pygame.sprite.collide_rect(WEA_,mons):
                    mons.be_hurt(WEA_)
                    # 怪物销毁
                    if mons.hp <= 0:
                        monsters_dead.add(mons) 
        hero.move()
        zhen.update(pygame.mouse.get_pos())
        hero.draw(screen)
        zhen.draw(screen)
        # 处理怪物与子弹相撞
        for mons in monsters_live:
            #mons.act(hero)
            for wea in weas_live:
                if pygame.sprite.collide_rect(mons,wea):
                    mons.be_hurt(wea)
                    # 子弹销毁
                    weas_dead.add(wea)
                    # 怪物销毁
                    if mons.hp <= 0:
                        monsters_dead.add(mons) 
        # 去除已经死去的怪物
        for mons in monsters_dead:
            monsters_live.remove(mons) 
        # 活着的怪物运动并绘制
        for mons in monsters_live: 
            mons.act(hero)
            mons.draw(screen)
        # 判定子弹越界
        for wea in weas_live:
            if wea.rect.bottom < 0 or wea.rect.top > cfg.SCREEN_SIZE[1] or wea.rect.left > cfg.SCREEN_SIZE[0] or wea.rect.right < 0:
                weas_dead.add(wea)
        # 销毁子弹越界
        for wea in weas_dead:
            weas_live.remove(wea)
        # 子弹运动并绘制
        for wea in weas_live:
            wea.update()
            wea.draw(screen)
        # 避免玄学，这里俩set都clear，收尸
        weas_dead.clear()
        monsters_dead.clear()
        if len(monsters_live) == 0:
            status = 1
            break
        if hero.hp <= 0:
            status = 2
            break
        # 绘制准星
        star.draw(screen)
        pygame.display.update()
        clock.tick(cfg.FPS)
    # 通关或失败
    if status == 1:
        pygame.mixer.music.stop()
        level2(screen,star)
    elif status == 2:
        pygame.mixer.music.stop()
        start_end.end(screen,star)

def level2(screen,star):
    '''
    展示画面：第二关
    加载地图
    生成Hero、EnemyA * cfg.ENEMYA_NUM_TWO、EnemyB * cfg.ENEMYB_NUM_TWO
    音乐
    定时事件：生成武器
    检测触碰(可放到object里面的类里去实现)
    人物死亡：跳转到end
    所有怪物死亡：跳转到level2
    '''
    # 显示第二关
    black_screen(screen,star,"第二关")
    # bgm and bgm
    bg_img = pygame.image.load(cfg.LEVEL2_BG_IMG_PATH)
    bg_img = pygame.transform.scale(bg_img,cfg.LEVEL2_BG_IMG_SIZE)
    bg_img_rect = bg_img.get_rect()
    pygame.mixer.music.load(cfg.BATTLE_BGM)
    pygame.mixer.music.play(-1)
    # 生成玩家
    hero = obj.Hero()
    hero.set_position((500,200))
    # 生成针管
    zhen = obj.Zhen(hero)
    # 生成怪物群
    monsters_live = set()
    for pos in cfg.MONSTERB_COORDS:
        monsterB = obj.MonsterB()
        monsterB.set_position(pos)
        monsters_live.add(monsterB)
    # 时钟  
    clock = pygame.time.Clock()
    # 子弹集合
    weas_live = set()
    # 炮弹集合 -------------------------------------
    paos_live = set()

    # 设置随机生成的强力回旋镖
    appear_event = pygame.USEREVENT
    pygame.time.set_timer(appear_event, cfg.WEAPEN_1_TIME)

    exist = False # 存在静态回旋镖
    WEA = 0 # 静态回旋镖
    hold = False # 人物获得了回旋镖

    exist_ = False # 存在动态回旋镖
    WEA_ = 0 # 动态回旋镖
    
    status = 0 # 通关（1）或者失败（2）的标志
    while True:
        weas_dead = set() # 销毁的子弹：杀人而销毁、越界而销毁
        monsters_dead = set() # 死去的怪物
        paos_dead = set() # 销毁的炮弹 -------------------------------
        screen.blit(bg_img,bg_img_rect)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == pygame.MOUSEMOTION:
                star.set_position(pygame.mouse.get_pos())
            elif event.type == pygame.MOUSEBUTTONDOWN:
                if event.button == 1:
                    pygame.mixer.Sound(cfg.BIU).play()
                    if hold:
                        WEA_ = obj.Weapen1()
                        WEA_.set_direction(zhen)
                        exist_ = True
                        hold = False
                    else:
                        new_weapen = obj.Weapen0()
                        new_weapen.set_direction(zhen)
                        weas_live.add(new_weapen)
            elif event.type == pygame.KEYDOWN:
                hero.press(event.key)
            elif event.type == pygame.KEYUP:
                hero.release(event.key)
            elif event.type == appear_event:
                if exist : break
                x = random.randint(100,cfg.SCREEN_SIZE[0]-100)
                y = random.randint(100,cfg.SCREEN_SIZE[1]-100)
                WEA = obj.Weapen_1_static((x,y))
                exist = True
        # 人物与静态回旋镖的互动：吃
        if exist:
            WEA.draw(screen)
            if pygame.sprite.collide_rect(WEA,hero):
                hold = True
                exist = False
        # 动态回旋镖的处理
        if exist_:
            WEA_.update()
            WEA_.draw(screen)
            # 回旋镖越界处理
            if WEA_.rect.bottom < 0 or WEA_.rect.top > cfg.SCREEN_SIZE[1] or WEA_.rect.left > cfg.SCREEN_SIZE[0] or WEA_.rect.right < 0:
                exist_ = False
            for mons in monsters_live:
                if pygame.sprite.collide_rect(WEA_,mons):
                    mons.be_hurt(WEA_)
                    # 怪物销毁
                    if mons.hp <= 0:
                        monsters_dead.add(mons) 
        hero.move()
        zhen.update(pygame.mouse.get_pos())
        hero.draw(screen)
        zhen.draw(screen)
        # 处理怪物与子弹相撞
        for mons in monsters_live:
            for wea in weas_live:
                if pygame.sprite.collide_rect(mons,wea):
                    mons.be_hurt(wea)
                    # 子弹销毁
                    weas_dead.add(wea)
                    # 怪物销毁
                    if mons.hp <= 0:
                        monsters_dead.add(mons) 
        # 去除已经死去的怪物
        for mons in monsters_dead:
            monsters_live.remove(mons) 
        # 活着的怪物运动并绘制--------------------
        for mons in monsters_live: 
            new_pao = mons.act(hero)
            if new_pao != 0:
                paos_live.add(new_pao)
            mons.draw(screen)
        # 判定子弹越界
        for wea in weas_live:
            if wea.rect.bottom < 0 or wea.rect.top > cfg.SCREEN_SIZE[1] or wea.rect.left > cfg.SCREEN_SIZE[0] or wea.rect.right < 0:
                weas_dead.add(wea)
        # 销毁子弹越界
        for wea in weas_dead:
            weas_live.remove(wea)
        # 子弹运动并绘制
        for wea in weas_live:
            wea.update()
            wea.draw(screen)
        # 炮弹的越界问题、销毁、绘制:
        for pao in paos_live:
            if pao.rect.bottom < 0 or pao.rect.top > cfg.SCREEN_SIZE[1] or pao.rect.left > cfg.SCREEN_SIZE[0] or pao.rect.right < 0:
                paos_dead.add(pao)
            if pygame.sprite.collide_rect(pao,hero):
                hero.hp -= cfg.MONSTERB_ATTACK_VALUE
                paos_dead.add(pao)
        for pao in paos_dead:
            paos_live.remove(pao)
        for pao in paos_live:
            pao.update()
            pao.draw(screen)
        # 避免玄学，这里俩set都clear，收尸
        weas_dead.clear()
        monsters_dead.clear()
        paos_dead.clear()
        if len(monsters_live) == 0:
            status = 1
            break
        if hero.hp <= 0:
            status = 2
            break
        # 绘制准星
        star.draw(screen)
        pygame.display.update()
        clock.tick(cfg.FPS)
    if status == 1:
        pygame.mixer.music.stop()
        level3(screen,star)
    elif status == 2:
        pygame.mixer.music.stop()
        start_end.end(screen,star)

def level3(screen,star):
    # 显示三关
    black_screen(screen,star,"Boss关")
    # bgm and bgm
    bg_img = pygame.image.load(cfg.LEVEL3_BG_IMG_PATH)
    bg_img = pygame.transform.scale(bg_img,cfg.LEVEL3_BG_IMG_SIZE)
    bg_img_rect = bg_img.get_rect()
    pygame.mixer.music.load(cfg.BATTLE_BGM)
    pygame.mixer.music.play(-1)
    # 生成玩家
    hero = obj.Hero()
    hero.set_position((500,200))
    # 生成针管
    zhen = obj.Zhen(hero)
    # 怪物群、boss
    monsters_live = set()
    # monsters_live.add(obj.MonsterA()) # test
    boss = obj.Boss()
    boss.set_position(cfg.BOSS_COORD)
    # 时钟  
    clock = pygame.time.Clock()
    # 子弹集合
    weas_live = set()
    # 设置随机生成的强力回旋镖
    appear_event = pygame.USEREVENT
    pygame.time.set_timer(appear_event, cfg.WEAPEN_1_TIME)

    exist = False # 存在静态回旋镖
    WEA = 0 # 静态回旋镖
    hold = False # 人物获得了回旋镖

    exist_ = False # 存在动态回旋镖
    WEA_ = 0 # 动态回旋镖
    
    status = 0 # 通关（1）或者失败（2）的标志
    while True:
        weas_dead = set() # 销毁的子弹：杀人而销毁、越界而销毁
        monsters_dead = set() # 死去的怪物
        screen.blit(bg_img,bg_img_rect)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == pygame.MOUSEMOTION:
                star.set_position(pygame.mouse.get_pos())
            elif event.type == pygame.MOUSEBUTTONDOWN:
                if event.button == 1:
                    pygame.mixer.Sound(cfg.BIU).play()
                    if hold:
                        WEA_ = obj.Weapen1()
                        WEA_.set_direction(zhen)
                        exist_ = True
                        hold = False
                    else:
                        new_weapen = obj.Weapen0()
                        new_weapen.set_direction(zhen)
                        weas_live.add(new_weapen)
            elif event.type == pygame.KEYDOWN:
                hero.press(event.key)
            elif event.type == pygame.KEYUP:
                hero.release(event.key)
            elif event.type == appear_event:
                if exist : break
                x = random.randint(100,cfg.SCREEN_SIZE[0]-100)
                y = random.randint(100,cfg.SCREEN_SIZE[1]-100)
                WEA = obj.Weapen_1_static((x,y))
                exist = True
        # 人物与静态回旋镖的互动：吃
        if exist:
            WEA.draw(screen)
            if pygame.sprite.collide_rect(WEA,hero):
                hold = True
                exist = False
        # 动态回旋镖的处理
        if exist_:
            WEA_.update()
            WEA_.draw(screen)
            # 回旋镖越界处理
            if WEA_.rect.bottom < 0 or WEA_.rect.top > cfg.SCREEN_SIZE[1] or WEA_.rect.left > cfg.SCREEN_SIZE[0] or WEA_.rect.right < 0:
                exist_ = False
            for mons in monsters_live:
                if pygame.sprite.collide_rect(WEA_,mons):
                    mons.be_hurt(WEA_)
                    # 怪物销毁
                    if mons.hp <= 0:
                        monsters_dead.add(mons)
            if pygame.sprite.collide_rect(WEA_,boss):
                boss.be_hurt(WEA_)
                exist_ = False
        
        hero.move()
        zhen.update(pygame.mouse.get_pos())
        hero.draw(screen)
        zhen.draw(screen)
        # 处理boss行为
        boss.act(monsters_live)
        for wea in weas_live:
            if pygame.sprite.collide_rect(wea,boss):
                boss.be_hurt(wea)
                weas_dead.add(wea)
        boss.draw(screen)

        # 处理怪物与子弹相撞
        for mons in monsters_live:
            #mons.act(hero)
            for wea in weas_live:
                if pygame.sprite.collide_rect(mons,wea):
                    mons.be_hurt(wea)
                    # 子弹销毁
                    weas_dead.add(wea)
                    # 怪物销毁
                    if mons.hp <= 0:
                        monsters_dead.add(mons) 
        # 判定怪物红血，攻击状态切换
        if boss.hp < 1/5 * cfg.BOSS_HP and not boss.red:
            boss.red_blood()
                
        # 去除已经死去的怪物
        for mons in monsters_dead:
            monsters_live.remove(mons) 
        # 活着的怪物运动并绘制
        for mons in monsters_live: 
            mons.act(hero)
            mons.draw(screen)
        # 判定子弹越界
        for wea in weas_live:
            if wea.rect.bottom < 0 or wea.rect.top > cfg.SCREEN_SIZE[1] or wea.rect.left > cfg.SCREEN_SIZE[0] or wea.rect.right < 0:
                weas_dead.add(wea)
        # 删除销毁了的子弹
        for wea in weas_dead:
            weas_live.remove(wea)
        # 子弹运动并绘制
        for wea in weas_live:
            wea.update()
            wea.draw(screen)
        # 避免玄学，这里俩set都clear，收尸
        weas_dead.clear()
        monsters_dead.clear()
        if boss.hp <= 0:
            status = 1
            break
        if hero.hp <= 0:
            status = 2
            break
        # 绘制准星
        star.draw(screen)
        pygame.display.update()
        clock.tick(cfg.FPS)
    if status == 1:
        pygame.mixer.music.stop()
        pygame.mixer.music.load(cfg.SUCCESS_BGM)
        pygame.mixer.music.play()
        stime = time.time()
        
        while True:
            screen.blit(bg_img,bg_img_rect)
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit()
                elif event.type == pygame.MOUSEMOTION:
                    star.set_position(pygame.mouse.get_pos())
            etime = time.time()
            zhen.update(pygame.mouse.get_pos())
            star.draw(screen)
            hero.draw(screen)
            zhen.draw(screen)
            pygame.display.update()
            if etime - stime >= 5:
                break
        pygame.mixer.music.stop()
        start_end.success(screen,star)
    elif status == 2:
        pygame.mixer.music.stop()
        start_end.end(screen,star)

